import gtk
import gtk.glade
import swisseph as swe
import math

def degminsec(value):
    val = value
    deg = int(val)
    x = math.floor(val)
    diff_min = val - x
    act_mins = ( diff_min * 60 )
    mins = int(act_mins)
    y = math.floor(act_mins)
    diff_sec = act_mins - y
    act_secs = ( diff_sec * 60 )
    secs = int(act_secs)
    return deg,mins,secs

def house_name(degree):
    get_deg = degree
    quo = get_deg / 30
    mod = get_deg % 30
    if quo == 0:
        quo = quo + 1
        sign_name = 'Mesham'
    elif quo == 1:
        quo = quo + 1
        sign_name = 'Rishabam'
    elif quo == 2:
        quo = quo + 1
        sign_name = 'Mithunam'
    elif quo == 3:
        quo = quo + 1
        sign_name = 'Kadagam'
    elif quo == 4:
        quo = quo + 1
        sign_name = 'Simam'
    elif quo == 5:
        quo = quo + 1
        sign_name = 'Kanni'
    elif quo == 6:
        quo = quo + 1
        sign_name = 'Tulam'
    elif quo == 7:
        quo = quo + 1
        sign_name= 'Viruchagam'
    elif quo == 8:
        quo = quo + 1
        sign_name = 'Thanusu'
    elif quo == 9:
        quo = quo + 1
        sign_name = 'Magaram'
    elif quo == 10:
        quo = quo + 1
        sign_name = 'Kumbam'
    elif quo == 11:
        quo = quo + 1
        sign_name = 'Meenam'
    else: 
        print 'No Sign'
    return mod, sign_name
           
class Astro_App:
    def __init__(self):
        self.gladefile = "astrogui.glade"
        self.wTree = gtk.glade.XML(self.gladefile)       
        dic = {
                "on_Main_Window_destroy":gtk.main_quit,
                "on_clk_clicked":self.process,
                "on_close_clicked":self.close,
                "on_Clear_clicked":self.clear,  
                "on_Rasi_Window_destroy":self.hide          
                }
        self.wTree.signal_autoconnect(dic)
        self.window = self.wTree.get_widget("Main_Window")
        self.rasi_window = self.wTree.get_widget("Rasi_Window")
        self.window.show()

    def process(self,widget):
        t = 0.00
        date = self.wTree.get_widget("date").get_text()
        month = self.wTree.get_widget("month").get_text()
        year = self.wTree.get_widget("year").get_text()
        time = self.wTree.get_widget("time").get_text()
        timezone = self.wTree.get_widget("timezone").get_text()
        latitude = self.wTree.get_widget("latitude").get_text()
        longitude = self.wTree.get_widget("longitude").get_text()
        jd = swe.julday(int(year),int(month),int(date),t,1)
        sun_pos = swe.calc_ut(jd,0,258)    
        su_deg,su_min,su_sec = degminsec(sun_pos[0])
        su_mod_val,su_sig_name = house_name(su_deg)    
        su_str = str(sun_pos[3])
        if su_str.startswith('-'):
             su_neg = '(R)'
             print 'Sun         %s  %d-%d\'%d\"         %d  %s  %d\'%d\"' % ( su_neg, su_deg, su_min, su_sec, su_mod_val, su_sig_name, su_min,su_sec )
        else:
            print 'Sun              %d-%d\'%d\"         %d  %s  %d\'%d\"' % ( su_deg, su_min, su_sec, su_mod_val, su_sig_name, su_min,su_sec)
        moon_pos = swe.calc_ut(jd,1,258)
        mo_deg, mo_min, mo_sec = degminsec(moon_pos[0])
        mo_mod_val, mo_sig_name = house_name(mo_deg) 
        mo_str = str(moon_pos[3])
        if mo_str.startswith('-'):
            mo_neg = '(R)'
            print 'Moon         %s  %d-%d\'%d\"         %d  %s  %d\'%d\"' % ( mo_neg, mo_deg, mo_min, mo_sec,  mo_mod_val, mo_sig_name, mo_min, mo_sec )
        else:
            print 'Moon             %d-%d\'%d\"         %d  %s  %d\'%d\"' % ( mo_deg, mo_min, mo_sec, mo_mod_val, mo_sig_name, mo_min, mo_sec )
        mercury_pos = swe.calc_ut(jd,2,258)
        me_deg, me_min, me_sec = degminsec(mercury_pos[0])
        me_mod_val, me_sig_name = house_name(me_deg)  
        me_str = str(mercury_pos[3])
        if me_str.startswith('-'):
            me_neg = '(R)'
            print 'Mercury          %s  %d-%d\'%d\"         %d  %s  %d\'%d\"' % ( me_neg, me_deg, me_min, me_sec, me_mod_val, me_sig_name, me_min, me_sec )
        else:
            print 'Mercury              %d-%d\'%d\"         %d  %s  %d\'%d\"' % (me_deg, me_min, me_sec, me_mod_val, me_sig_name, me_min, me_sec )
        venus_pos   = swe.calc_ut(jd,3,258)
        ve_deg, ve_min, ve_sec = degminsec(venus_pos[0])
        ve_mod_val, ve_sig_name = house_name(ve_deg)
        ve_str = str(venus_pos[3])       
        if ve_str.startswith('-'):
            ve_neg = '(R)'
            print 'Venus            %s  %d-%d\'%d\"         %d  %s  %d\'%d\"' % ( ve_neg, ve_deg, ve_min, ve_sec,  ve_mod_val, ve_sig_name, ve_min, ve_sec )
        else:
            print 'Venus                %d-%d\'%d\"         %d  %s  %d\'%d\"' % ( ve_deg, ve_min, ve_sec, ve_mod_val, ve_sig_name, ve_min, ve_sec )
        mars_pos = swe.calc_ut(jd,4,258)
        ma_deg, ma_min, ma_sec = degminsec(mars_pos[0])
        ma_mod_val, ma_sig_name = house_name(ma_deg)  
        ma_str = str(mars_pos[3])
        if ma_str.startswith('-'):
            ma_neg = '(R)'
            print 'Mars         %s  %d-%d\'%d\"         %d  %s  %d\'%d\"' % ( ma_neg, ma_deg, ma_min, ma_sec, ma_mod_val, ma_sig_name, ma_min, ma_sec)
        else:
            print 'Mars             %d-%d\'%d\"         %d  %s  %d\'%d\"'  % (ma_deg, ma_min, ma_sec, ma_mod_val, ma_sig_name, ma_min, ma_sec  )
        jupiter_pos = swe.calc_ut(jd,5,258)
        ju_deg,ju_min,ju_sec = degminsec(jupiter_pos[0])
        ju_mod_val, ju_sig_name = house_name(ju_deg)        
        ju_str = str(jupiter_pos[3])
        if ju_str.startswith('-'):
            ju_neg = '(R)'
            print 'Jupiter          %s  %d-%d\'%d\"         %d  %s  %d\'%d\"' % (  ju_neg, ju_deg, ju_min, ju_sec, ju_mod_val, ju_sig_name, ju_min, ju_sec )
        else:
            print 'Jupiter              %d-%d\'%d\"         %d  %s  %d\'%d\"' % ( ju_deg, ju_min, ju_sec, ju_mod_val, ju_sig_name, ju_min, ju_sec )
        saturn_pos = swe.calc_ut(jd,6,258)
        sa_deg, sa_min, sa_sec = degminsec(saturn_pos[0])
        sa_mod_val, sa_sig_name = house_name(sa_deg)        
        sa_str = str(saturn_pos[3])
        if sa_str.startswith('-'):
            sa_neg = '(R)'
            print 'Saturn           %s  %d-%d\'%d\"         %d  %s  %d\'%d\"' % (  sa_neg, sa_deg, sa_min, sa_sec, sa_mod_val, sa_sig_name, sa_min, sa_sec )
        else:
            print 'Saturn               %d-%d\'%d\"         %d  %s  %d\'%d\"' % ( sa_deg, sa_min, sa_sec, sa_mod_val, sa_sig_name, sa_min, sa_sec )
        northnode_pos = swe.calc_ut(jd,7,258)
        southnode_pos = swe.calc_ut(jd,8,258)
        self.rasi_window.show()
  

    def clear(self,widget):
        self.wTree.get_widget("date").set_text("")
        self.wTree.get_widget("month").set_text("")
        self.wTree.get_widget("year").set_text("")
        self.wTree.get_widget("time").set_text("")
        self.wTree.get_widget("timezone").set_text("")
        self.wTree.get_widget("latitude").set_text("")
        self.wTree.get_widget("longitude").set_text("")

    def hide(self,widget):
        self.rasi_window.hide()


    def close(self,widget):
        gtk.main_quit()

if __name__=="__main__":
    a = Astro_App()
    gtk.main()
